package com.du.linandroid.biz;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.du.linandroid.util.Constant;
import com.du.linandroid.util.LLog;
import com.du.linandroid.R;
import com.du.linandroid.callback.ShareCallBack;
import com.du.linandroid.util.Util;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import java.util.ArrayList;



public class QQShare {
    private Activity mContext;
    private Tencent mTencent;
    private ShareCallBack callBack;
    public QQShare(Activity mContext){
        if(ApiHolder.mTencent == null){
            Log.e("linandroid" , "使用前必须调用Lin.init()");
        }else{
            this.mContext = mContext;
            mTencent = ApiHolder.mTencent;
        }

    }


    public void shareQQ(String title, String text, String url, String imageUrl ,ShareCallBack callBack ) {
        if(callBack != null){
            this.callBack = callBack;
        }
        final Bundle params = new Bundle();
        params.putInt(com.tencent.connect.share.QQShare.SHARE_TO_QQ_KEY_TYPE,
                com.tencent.connect.share.QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_TITLE, title);
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_SUMMARY, text);
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_TARGET_URL,
                url);
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_IMAGE_URL,
                imageUrl);
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
        params.putString(com.tencent.connect.share.QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
        mTencent.shareToQQ(mContext, params, shareListener);
    }

    public void shareQQZone(String title, String text, String url,  String imageUrl ,ShareCallBack callBack ) {
        if(callBack != null){
            this.callBack = callBack;
        }
        Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 标题
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, text);// 摘要 　　
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, url);// 内容地址 　　
        ArrayList<String> imgUrlList = new ArrayList<>();
        imgUrlList.add(imageUrl);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imgUrlList);// 图片地址
        mTencent.shareToQzone(mContext, params, shareListener);
    }

    private  IUiListener shareListener = new IUiListener() {
        @Override
        public void onComplete(Object o) {
            callBack.success((JSONObject) o);
            LLog.d("111111111");
        }

        @Override
        public void onError(UiError uiError) {
            callBack.fail(Util.getFailJson(uiError.errorCode+"" , uiError.errorMessage));
            LLog.d("2222222");
        }

        @Override
        public void onCancel() {
            callBack.fail(Util.getFailJson(Constant.QQ_SHARE_FAILED_USER_CANCEL_CODE , Constant.QQ_SHARE_FAILED_USER_CANCEL_STR));
        }
    };

}
